package com.bhprod.guitartrack.widget;

import java.util.Locale;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.SeekBar;

public class TimeSeekBar extends SeekBar {

	private Paint p;

	public TimeSeekBar(Context context) {
		super(context);

		createTextPaint();
	}

	private void createTextPaint() {
		p = new Paint();
		p.setColor(Color.BLACK);
		p.setAntiAlias(true);
	}

	public TimeSeekBar(Context context, AttributeSet attrs) {
		super(context, attrs);

		createTextPaint();
	}

	public TimeSeekBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		createTextPaint();
	}

	@Override
	protected synchronized void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		int progress = getProgress();

		String time = formatTimeProgress(progress);

		canvas.drawText(time, getThumb().getBounds().centerX()
				- getThumb().getBounds().width() / 4, getThumb().getBounds()
				.centerY() + 30, p);
	}

	public static String formatTimeProgress(int progress) {
		String time = String.format(
				"%02d:%02d",
				TimeUnit.MILLISECONDS.toMinutes(progress),
				TimeUnit.MILLISECONDS.toSeconds(progress)
						- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
								.toMinutes(progress)), Locale.getDefault());
		return time;
	}

}
